package com.wxtwxt.ruralmall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.wxtwxt.ruralmall.common.utils.BizCodeEnum;
import com.wxtwxt.ruralmall.common.utils.R;
import com.wxtwxt.ruralmall.member.exception.EmailExistsException;
import com.wxtwxt.ruralmall.member.exception.UserNameExistsException;
import com.wxtwxt.ruralmall.member.vo.UserLoginVo;
import com.wxtwxt.ruralmall.member.vo.UserRegisterVo;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import com.wxtwxt.ruralmall.member.entity.MemberEntity;
import com.wxtwxt.ruralmall.member.service.MemberService;
import com.wxtwxt.ruralmall.common.utils.PageUtils;

import javax.annotation.Resource;



/**
 * 会员
 *
 * @author Wxt
 * @email wxt23787@126.com
 * @date 2023-01-30 22:49:01
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Resource
    private MemberService memberService;

    @PostMapping("/loginWithThreeParty")
    public R login(@RequestParam("json") String json) {
        MemberEntity member = null;
        try {
            member = memberService.login(json);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if(member != null) {
            return R.ok().put("member", member);
        }
        return R.error(BizCodeEnum.USERACCOUNT_PASSWORD_ERROR.getCode(), BizCodeEnum.USERACCOUNT_PASSWORD_ERROR.getMessage());
    }

    @PostMapping("/login")
    public R login(@RequestBody UserLoginVo userLoginVo) {
        MemberEntity member = memberService.login(userLoginVo);
        if(member != null) {
            return R.ok().put("member", member);
        }
        return R.error(BizCodeEnum.USERACCOUNT_PASSWORD_ERROR.getCode(), BizCodeEnum.USERACCOUNT_PASSWORD_ERROR.getMessage());
    }

    @PostMapping("/register")
    public R register(@RequestBody UserRegisterVo userRegisterVo) {
        try {
            memberService.register(userRegisterVo);
            return R.ok();
        }catch(UserNameExistsException e) {
            return R.error(BizCodeEnum.USERNAME_EXIST_EXCPTION.getCode(), BizCodeEnum.USERNAME_EXIST_EXCPTION.getMessage());
        }catch (EmailExistsException e) {
            return R.error(BizCodeEnum.EMAIL_EXIST_EXCPTION.getCode(), BizCodeEnum.EMAIL_EXIST_EXCPTION.getMessage());
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
